<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件类型" prop="fileTypeId">
        <el-select v-model="queryParams.fileTypeId" clearable placeholder="请输入文件类型" @change="handleQuery">
          <el-option
            v-for="item in fileTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学生名字" prop="studentId">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="beginTime">
        <el-date-picker
          @change="handleQuery"
          :editable="false"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          v-model="queryParams.beginTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          @change="handleQuery"
          :editable="false"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          v-model="queryParams.endTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出资料</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bmxt:files:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bmxt:files:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bmxt:files:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table v-loading="loading" :data="filesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="文件主键" align="center" prop="fileId" />-->
      <el-table-column label="文件图片" align="center" prop="filePath">
        <template slot-scope="scope">
          <img :src="calcImg(scope.row.filePath)" width="50" height="50">
        </template>
      </el-table-column>
      <el-table-column label="文件类型" align="center" prop="fileTypeId">
        <template slot-scope="scope">
          <span v-for="item in fileTypeOptions">
             <span v-if="scope.row.fileTypeId == item.dictValue">{{item.dictLabel}}</span>
          </span>
         <!-- <span v-if="scope.row.fileTypeId == 1">证件照</span>
          <span v-if="scope.row.fileTypeId == 2">身份证正面</span>
          <span v-if="scope.row.fileTypeId == 3">身份证反面</span>
          <span v-if="scope.row.fileTypeId == 4">前置毕业证</span>
          <span v-if="scope.row.fileTypeId == 5">证明材料</span>-->
        </template>
      </el-table-column>
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="推荐人" align="center" prop="nickName" />
      <el-table-column label="审核状态" align="center" prop="nickName">
        <template slot-scope="scope">
          <span v-for="item in auditStatusOptions">
            <span v-if="item.dictValue == scope.row.auditStatus"
                  :class="{success:item.dictValue == 1 ,error:item.dictValue == 2,warn:item.dictValue == 0}">{{item.dictLabel}}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bmxt:files:edit']"
          >查看</el-button>
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bmxt:files:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件资料管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件路径" prop="filePath">
         <!-- <el-input v-model="form.filePath" placeholder="请输入文件路径" />-->
          <img :src="calcImg(form.filePath)" width="100" height="100">
        </el-form-item>
        <el-form-item label="文件类型" prop="fileTypeId">
          <el-select v-model="form.fileTypeId" :disabled="true" clearable placeholder="请输入文件类型">
            <el-option
              v-for="item in fileTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>-->
        <!--<el-form-item label="学生主键" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生主键" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFiles, getFiles, delFiles, addFiles, updateFiles,exportZip } from "@/api/bmxt/files";
import {getDicts} from "@/api/system/dict/data";
import {getLoginUserId} from "@/api/system/user";
export default {
  name: "Files",
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
      fileTypeOptions:[],
      auditStatusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件资料管理表格数据
      filesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        filePath: null,
        fileTypeId: null,
        studentId: null,
        studentName: null,
        userId: '',
        beginTime: '',
        endTime: ''
      },
      fileQueryParams: {
        userId: '',
        beginTime: '',
        endTime: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    getDicts('bmxt_file_type').then(resp=>{
      console.log("材料类型",resp)
      this.fileTypeOptions = resp.data;
    })
    getDicts('bmxt_student_audit_status').then(resp=>{
      console.log("审核状态",resp)
      this.auditStatusOptions = resp.data;
    })
  },
  methods: {
    calcImg(filePath){
      return process.env.VUE_APP_BASE_API + filePath
    },
    /** 查询文件资料管理列表 */
    getList() {
      this.loading = true;
      listFiles(this.queryParams).then(response => {
        this.filesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        filePath: null,
        fileTypeId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        studentId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    fileResetQuery() {
      this.resetForm("fileQueryForm");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fileId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件资料管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fileId = row.fileId || this.ids
      getDicts('bmxt_file_type').then(resp=>{
        console.log("材料类型",resp)
        this.fileTypeOptions = resp.data;
        getFiles(fileId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改文件资料管理";
        });
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fileId != null) {
            updateFiles(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFiles(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fileIds = row.fileId || this.ids;
      console.log("删除的id信息",fileIds)
      console.log("删除的id信息",typeof fileIds)
      this.$modal.confirm('是否确认删除文件资料管理编号为"' + fileIds + '"的数据项？').then(function() {
        return delFiles(fileIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    async handleExport() {
      this.$modal.confirm('是否确认导出文件资料？').then(()=> {
        console.log(this.queryParams)
        let userId = '';
        getLoginUserId().then(resp=>{
            console.log("获得登录信息：",resp)
            if(resp.code == 200){
              userId = resp.data;
              this.queryParams.userId = userId;
              window.location.href =  process.env.VUE_APP_BASE_API+"/noAuth/web/student/createFileByUser?userId="
                +this.queryParams.userId+"&beginTime="+this.queryParams.beginTime+"&endTime="+
                this.queryParams.endTime
            }
          }
        )
      }).then(() => {
        this.$modal.msgSuccess("导出成功");
      }).catch(() => {});

    }
  }
};
</script>

<style scoped>
  .success{
    color: green;
    font-weight: bold;
  }
  .error{
    color: red;
    font-weight: bold;
  }
  .warn{
    color: blue;
    font-weight: bold;
  }
</style>
